//
//  LoginViewController.m
//  IMDome
//
//  Created by limaohuyu on 2020/8/13.
//  Copyright © 2020 czw. All rights reserved.
//

#import "LoginViewController.h"
#import "RegisterViewController.h"
#import "BaseServiceManager.h"
#import "BaseTabBarController.h"
#import "UserModel.h"

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *nameTextField;
@property (weak, nonatomic) IBOutlet UITextField *pwTextField;

@end

@implementation LoginViewController

- (void)viewWillAppear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:YES animated:animated];
    [super viewWillAppear:animated];
}
- (void)viewWillDisappear:(BOOL)animated {
    [self.navigationController setNavigationBarHidden:NO animated:animated];
    [super viewWillDisappear:animated];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
}
- (IBAction)loginAction:(id)sender {
    if (kStringIsEmpty(self.nameTextField.text)) {
        [AnimationView showString:@"账号不能为空"];
        return;
    }
    if (kStringIsEmpty(self.pwTextField.text)) {
        [AnimationView showString:@"密码不能为空"];

        return;
    }
    NSMutableDictionary *params  = [NSMutableDictionary dictionary];
    [params setObject:self.nameTextField.text forKey:@"account"];
    [params setObject:self.pwTextField.text forKey:@"password"];
    [BaseServiceManager getRequest:@"user/login" params:params startBlock:^{
        [self showRequestHUDView];
    } successBlock:^(ResponseBaseModel * _Nonnull responseModel) {
        [self hideRequestHUDView];

        NSDictionary*dataDic = responseModel.data;
        NSString *token =   dataDic[@"token"];
        if (!kStringIsEmpty(token)) {
            [[NSUserDefaults standardUserDefaults] setObject:token forKey:IM_TOKEN_KEY];
            [APPUserDataManager shareInstance].token = token;
        }
        UserModel *userModel= [UserModel mj_objectWithKeyValues:dataDic[@"user"]];
        if (!kStringIsEmpty(userModel.userId)) {
            [[NSUserDefaults standardUserDefaults] setObject:userModel.userId forKey:IM_USER_ID_KEY];
            [APPUserDataManager shareInstance].userId = userModel.userId;

        }
        
        [[NSNotificationCenter defaultCenter] postNotificationName:USER_LOGIN_SUCCESS_KEY object:nil];
        
    } failureBlock:^(NSError * _Nonnull error, NSString * _Nonnull msg) {
        [self hideRequestHUDView];

    }];
    
    
}
- (IBAction)rgItemAction{
    
    [self.navigationController pushViewController:[RegisterViewController new] animated:YES];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
@end
